﻿namespace Whut.ObjectSharePointMapper.Internals
{
    using System;
    using Microsoft.SharePoint;

    public class SharePointSession : ISharePointSession
    {
        private MetaDataStore metaDataStore;

        private SessionLevelCache sessionLevelCache;

        private EntityHydrater entityHydrater;

        private UpdateAction updateAction;

        private DeleteAction deleteAction;

        public SharePointSession(MetaDataStore metaDataStore)
        {
            this.metaDataStore = metaDataStore;
            this.sessionLevelCache = new SessionLevelCache();
            this.entityHydrater = new EntityHydrater(this.metaDataStore, this.sessionLevelCache);
            this.updateAction = new UpdateAction(this.metaDataStore, this.sessionLevelCache, this.entityHydrater);
            this.deleteAction = new DeleteAction(this.metaDataStore, this.sessionLevelCache, this.entityHydrater);
        }

        public TEntity Wrap<TEntity>(SPListItem item)
        {
            return this.entityHydrater.Hydrate<TEntity>(item);
        }

        public void Update<TEntity>(TEntity entity)
        {
            this.updateAction.Update(entity);
        }

        public void Delete<TEntity>(TEntity entity)
        {
            this.deleteAction.Delete(entity);
        }
    }
}
